package com.lagou.controller;

import com.lagou.config.CorsConfig;
import com.lagou.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.HttpCookie;

@Controller
@RequestMapping("/api")
@Slf4j
public class UserController {
    @Autowired
    private UserService service;

    @RequestMapping("/user/register/{email}/{password}/{code}")
    @ResponseBody
    public boolean registerUser(@PathVariable("email") String email, @PathVariable("password") String password,
                                @PathVariable("code") String code) {
        log.info("registerUser 开始");
        return service.registerUser(email, password, code);
    }

    @RequestMapping("/user/isRegistered/{email}")
    @ResponseBody
    public boolean isRegistered(@PathVariable("email") String email) {
        return service.isRegistered(email);
    }

    /*@RequestMapping("/user/login/{email}/{password}")
    //@ResponseBody
    public void login(@PathVariable("email") String email, @PathVariable("password") String password, HttpServletResponse response) throws IOException {
        String token = service.login(email, password);
        if (StringUtils.isEmpty(token)) {
            //return "false";
            response.getWriter().write("false");
        } else {
            Cookie cookie = new Cookie("token", token);
            //response.setHeader("token", token);
            cookie.setPath("/");
            cookie.setMaxAge(60*60*24*365);

            response.addCookie(cookie);
            response.setHeader("Access-Control-Allow-Origin", "*");
            //response.setHeader("Access-Control-Allow-Credentials", "true");
            response.getWriter().write(email);
        }
        //return email;
    }*/

    @GetMapping("/user/login/{email}/{password}")
    @ResponseBody
    public String login(
            @PathVariable("email") String email,
            @PathVariable("password") String password,
            HttpServletResponse response){
        String token = service.login(email, password);
        if (StringUtils.isEmpty(token)) {
            return "false";
            //response.getWriter().write("false");
        }
        Cookie cookie = new Cookie("token", token);
        cookie.setPath("/");
        response.addCookie(cookie);
        response.setHeader("Access-Control-Allow-Origin", "*");
        return email;
    }


    @RequestMapping("/user/info/{token}")
    @ResponseBody
    public String userInfo(@PathVariable String token , HttpServletResponse response){
        log.info("login token:" + token);
        String email = "";
        try {
            email = service.getEmail(token);
        }catch (Exception e){
            log.info("error", e);
            email = "failed";
        }
        log.info("login email:" + email);
        response.setHeader("Access-Control-Allow-Origin", "*");
        return email;
    }

}
